﻿using System;
using System.Data;
using System.Collections;
using System.Data.SqlClient;

namespace Project_KhuyenMaiVang.DataProvider
{
    public class MyDataProvider
    {
        protected string _connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;
                                                            Integrated Security=True;User Instance=True";

        protected SqlConnection _Connection;
        protected SqlDataAdapter _Adapter;
        protected SqlCommand _Command;
        protected SqlDataReader _DataReader;


        public string ConnectionString
        {
            get
            {
                return _connectionString;
            }
            set
            {
                _connectionString = value;
            }
        }

        public MyDataProvider()
        {
            this.connect();
        }

        protected void connect()
        {
            _Connection = new SqlConnection(ConnectionString);
            _Connection.Open();
        }

        protected void disconnect()
        {
            _Connection.Close();
        }

        protected SqlDataReader executeQuery(string sqlString)
        {
            _Command = new SqlCommand(sqlString, _Connection);
            return _Command.ExecuteReader();
        }

        //public DataTable executeQuery(string sqlString)
        //{
        //    DataSet ds = new DataSet();
        //    adapter = new OleDbDataAdapter(sqlString, connection);
        //    adapter.Fill(ds);
        //    return ds.Tables[0];
        //}

        protected void executeNonQuery(string sqlString)
        {
            _Command = new SqlCommand(sqlString, _Connection);
            _Command.ExecuteNonQuery();
        }

        protected object executeScalar(string sqlString)
        {
            _Command = new SqlCommand(sqlString, _Connection);
            return _Command.ExecuteScalar();
        }

        protected ArrayList ConvertDataSetToArrayList(DataSet dataset)
        {
            ArrayList arr = new ArrayList();
            DataTable dt = dataset.Tables[0];
            int i, n = dt.Rows.Count;
            for (i = 0; i < n; i++)
            {
                object obj = GetDataFromDataRow(dt, i);
                arr.Add(obj);

            }
            return arr;
        }

        protected virtual object GetDataFromDataRow(DataTable dt, int i)
        {
            return null;
        }
    }
}